var basePage = require('../../../common/BasePage');
Page({
    behaviors: [basePage],
    data: {
        detail_info: {},
        addr: {}
    },
    onLoad: function(options) {
        let that = this
        const { id } = options
        this.request({
            url: this.data.host + '/home/mall/preInfo',
            data: {
                mall_product_id: id,
                nums: 1
            },
            loadingTip: '加载中'
        }, function(res) {
            let detail_info= res.data;
            detail_info.thumb=that.get_thumb(detail_info.photo);
            that.setData({ detail_info, addr: res.data.m_addr })
        })
    },
    changeAddr() {
        let that = this
        wx.navigateTo({
            url: "/pages/addr/AddrList",
            events: {
                change_addr(data) {
                    that.setData({ addr: data.addr })
                }
            }
        })
    },
    onSubmit() {
        if (!this.data.addr) {
            return wx.showToast({
                title: '请选择收货地址',
                icon: 'none'
            })
        }
        const { detail_info } = this.data
        this.request({
            url: this.data.host + '/home/mall/handle',
            method: 'POST',
            data: {
                mall_product_id: detail_info.mall_product_id,
                nums: detail_info.nums,
                addr_id: this.data.addr.addr_id
            },
            loadingTip: '订单提交中'
        }, function() {
            wx.showToast({
                title: '商品兑换成功',
                icon: 'success'
            })
            wx.navigateTo({
              url: '/pages/integral/integral',
            })
        })
    }
})